命令行参数
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
有时候你希望运行一个程序时候再传递给它消息,这要靠传递命令行参数给main()函数实现,命令行参数是在执行程序时候紧跟在程序名字后面的信息。
代码实例
下面的程序打印所有的命令行参数:
public class CommandLine {
public static void main(String args[]) {
for (int i = 0; i < args.length; i++){
System.out.println("args[" + i + "]: " + args[i]);
}
}
}
如下所示,运行这个程序:
java CommandLine this is a command line 200 -100
运行结果如下:
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100
在线练习
{$ activeFileHint $}